﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd2016r
/// 파  일  명 : Crd2016r.aspx.cs
/// 설      명 : 지출순위챠트(지출구분별 사용횟수 및 총액 리스트)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-11-11 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Crd.Exe;


namespace Updms.Crd
{
	public partial class Crd2016r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Crd2016r";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);
		CommCode.Crd crd = new CommCode.Crd(LANG_SET);

		//검색
		protected string kw1 = "";	//카드코드
		protected string kw2 = "";	//검색시작일
		protected string kw3 = "";	//검색종료일
		protected string kw4 = "";	//CRD
		protected string kw5 = "";	//AG
		protected string kw6 = "";	//정렬
		protected string kw7 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetCardInfo();
			SetOrderByList();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//달력콘트롤 설정
			ce_search_biz_from_dt.SelectedDate = DateTime.Now.AddMonths(-1);//1개월전
			ce_search_biz_to_dt.SelectedDate = DateTime.Now.AddMonths(0);//현재

			txt_search_biz_from_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_biz_to_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			string ls_card_cd = "";
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
						{
							if (cbl_search_card_cd.Items[i].Selected == true)
							{
								ls_card_cd += cbl_search_card_cd.Items[i].Value + ",";
							}
						}
						kw1 = ls_card_cd.Substring(0, ls_card_cd.LastIndexOf(","));
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						//kw1의 내용을 체크박스리스트에 바인딩
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_biz_from_dt.Text))
						{
							kw2 = DateUtil.GetMonthEx(-1);	// 오늘부터 1개월전
							txt_search_biz_from_dt.Text = kw2;
						}
						else
						{
							kw2 = txt_search_biz_from_dt.Text.Trim();
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_biz_from_dt.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_biz_to_dt.Text))
						{
							kw3 = DateUtil.GetMonthEx(0);	// 오늘
							txt_search_biz_to_dt.Text = kw3;
						}
						else
						{
							kw3 = txt_search_biz_to_dt.Text.Trim();
						}
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_biz_to_dt.Text = kw3;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw6"])) // url에 값이 없으면
					{
						kw6 = ddl_search_order_by.SelectedValue;
					}
					else
					{
						kw6 = Server.UrlDecode(Request.QueryString["kw6"]);
						ddl_search_order_by.SelectedValue = kw6;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw7"]))	// url에 값이 없으면
					{
						kw7 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw7 = Server.UrlDecode(Request.QueryString["kw7"]);
						ddl_search_chart_width.SelectedValue = kw7;
					}
					
					break;
				case "SEARCH":
					for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
					{
						if (cbl_search_card_cd.Items[i].Selected == true)
						{
							ls_card_cd += cbl_search_card_cd.Items[i].Value + ",";
						}
					}
					kw1 = ls_card_cd.Substring(0, ls_card_cd.LastIndexOf(","));
					kw2 = String.IsNullOrEmpty(txt_search_biz_from_dt.Text) ? DateUtil.GetMonthEx(-1) : txt_search_biz_from_dt.Text.Trim();
					kw3 = String.IsNullOrEmpty(txt_search_biz_to_dt.Text) ? DateUtil.GetMonthEx(0) : txt_search_biz_to_dt.Text.Trim();
					kw6 = ddl_search_order_by.SelectedValue;
					kw7 = ddl_search_chart_width.SelectedValue;
					break;
			}

			kw4 = cb_support_yn.Checked ? "CRD" : "";
			kw5 = cb_support_yn.Checked ? "AG" : "";
		}


		#endregion

		#region 마스터 Method (SetCardInfo, SetYearMonth, SetOrderByList, SetChartWidth)

		//카드정보
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			cbl_search_card_cd.Items.Clear();
			cbl_search_card_cd.DataSource = lo_ds;
			cbl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			cbl_search_card_cd.DataTextField = "Card_Nm_Color";
			cbl_search_card_cd.DataValueField = "Card_Cd";
			cbl_search_card_cd.DataBind();

			if (cbl_search_card_cd.Items.Count > 0)
			{
				cbl_search_card_cd.Items[0].Selected = true;
			}
		}

		//검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			txt_search_biz_from_dt.Text = DateUtil.GetMonthEx(li_term);
			txt_search_biz_to_dt.Text = DateUtil.GetMonthEx(0);
		}

		//정렬순서
		private void SetOrderByList()
		{
			crd.SetOrderByList(ddl_search_order_by);
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);

			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion

		#region 디테일 Method ()

		#endregion

		#region 챠트 (DrawCountNTotalAmountByAccount, SearchProcess)

		//조회
		private void DrawCountNTotalAmountByAccount(DataPack lo_dp)
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();
			
			//초기화
			chart_count_by_account.Data.Clear();

			//시리즈 갯수
			chart_count_by_account.Data.Series = 2;

			//넓이
			chart_count_by_account.Width = Convert.ToInt32(kw7);
			
			//합계금액:0, 사용횟수순:1
			if (ddl_search_order_by.SelectedValue == "Amount")
			{
				chart_count_by_account.Series[0].PointLabels.Visible = true;
				chart_count_by_account.Series[1].PointLabels.Visible = false;
			}
			else
			{
				chart_count_by_account.Series[0].PointLabels.Visible = false;
				chart_count_by_account.Series[1].PointLabels.Visible = true;
			}

			using (Crd2016r_NT _Crd2016r_NT = new Crd2016r_NT())
			{
				lo_ds.Merge(_Crd2016r_NT.GetCountNTotalAmountByAccount(lo_dp));
				li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");
			}

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_count_by_account.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", i, "Amount");
				chart_count_by_account.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", i, "Cnt");

				//X축 라벨링
				chart_count_by_account.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", i, "Account_Nm");

				//0은 안보여준다
				chart_count_by_account.Points[0, i].PointLabels.Visible = (chart_count_by_account.Data[0, i] > 0);
				chart_count_by_account.Points[1, i].PointLabels.Visible = (chart_count_by_account.Data[1, i] > 0);
			}
		}

		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//검색
			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_biz_dt_from", typeof(string), kw2);
			lo_dp.AddProperty("ls_biz_dt_to", typeof(string), kw3);
			lo_dp.AddProperty("ls_exc_sys_id", typeof(string), kw4);
			lo_dp.AddProperty("ls_exc_prefix", typeof(string), kw5);
			lo_dp.AddProperty("ls_order_by", typeof(string), kw6);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			DrawCountNTotalAmountByAccount(lo_dp);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//1개월
		protected void sbtn_1_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-1);
		}

		//3개월
		protected void sbtn_3_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-3);
		}

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-6);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			SetSearchParameter();
			
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			
			using (Crd1011m_NT _Crd1011m_NT = new Crd1011m_NT())
			{
				lo_ds.Merge(_Crd1011m_NT.GetMinNMaxBizDt(lo_dp));
				txt_search_biz_from_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt");
				txt_search_biz_to_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Max_Dt");
			}
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion

	}
}
